//
//  ZhuangCheMainHeadView.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2021/4/12.
//  Copyright © 2021 吕晶. All rights reserved.
//

#import "ZhuangCheMainHeadView.h"

@interface ZhuangCheMainHeadView ()

@property (nonatomic, strong) UILabel *line1;
@property (nonatomic, strong) UILabel *NOTitleLab;
@property (nonatomic, strong) UILabel *NOLab;
@property (nonatomic, strong) UILabel *NumTitleLab;
@property (nonatomic, strong) UILabel *NumLab;
@property (nonatomic, strong) UILabel *line2;
@property (nonatomic, strong) UILabel *boxTitleLab;
@property (nonatomic, strong) UIView *boxView;

@end

@implementation ZhuangCheMainHeadView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = HexColor(0xffffff);
        self.line1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 1)];
        self.line1.backgroundColor = Color_backgroundColor;
        [self addSubview:self.line1];
        
        self.NOTitleLab = [UILabel new];
        self.NOTitleLab.textColor = HexColor(0x9C9DB3);
        self.NOTitleLab.font = [UIFont systemFontOfSize:14];
        self.NOTitleLab.text = @"提货单号：";
        [self addSubview:self.NOTitleLab];
        [self.NOTitleLab sizeToFit];
        self.NOTitleLab.nim_left = 15;
        self.NOTitleLab.nim_top = 10;
        
        self.NOLab = [UILabel new];
        self.NOLab.textColor = HexColor(0x2B2C44);
        self.NOLab.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.NOLab];
        
        self.NumTitleLab = [UILabel new];
        self.NumTitleLab.textColor = HexColor(0x9C9DB3);
        self.NumTitleLab.font = [UIFont systemFontOfSize:14];
        self.NumTitleLab.text = @"车牌号：";
        [self addSubview:self.NumTitleLab];
        [self.NumTitleLab sizeToFit];
        self.NumTitleLab.nim_right = self.NOTitleLab.nim_right;
        self.NumTitleLab.nim_top = self.NOTitleLab.nim_bottom + 10;
        
        self.NumLab = [UILabel new];
        self.NumLab.textColor = HexColor(0x2B2C44);
        self.NumLab.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.NumLab];
        
        self.line2 = [[UILabel alloc] initWithFrame:CGRectMake(0, self.NumTitleLab.nim_bottom + 10, ScreenW, 1)];
        self.line2.backgroundColor = Color_backgroundColor;
        [self addSubview:self.line2];
        
        self.boxTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(15, self.line2.nim_bottom + 10, ScreenW - 30, 17)];
        self.boxTitleLab.textColor = HexColor(0x2B2C44);
        self.boxTitleLab.font = BoldFontOfSize(16);
        [self addSubview:self.boxTitleLab];
        
        self.boxView = [[UIView alloc] initWithFrame:CGRectMake(15, self.boxTitleLab.nim_bottom + 10, ScreenW - 30, 0)];
        self.boxView.backgroundColor = HexColor(0xffffff);
        [self addSubview:self.boxView];
    }
    return self;
}

-(void)setModel:(ZhuangCheMainModel *)model {
    _model = model;
    [self configUI];
}

- (void)configUI {
    self.NOLab.text = self.model.dp_num;
    [self.NOLab sizeToFit];
    self.NOLab.nim_left = self.NOTitleLab.nim_right;
    self.NOLab.nim_centerY = self.NOTitleLab.nim_centerY;
    
    self.NumLab.text = self.model.car_num;
    [self.NumLab sizeToFit];
    self.NumLab.nim_left = self.NumTitleLab.nim_right;
    self.NumLab.nim_centerY = self.NumTitleLab.nim_centerY;
    
    self.boxTitleLab.text = [NSString stringWithFormat:@"周转箱清单 (共%lu个)",(unsigned long)self.model.pk_name.count];
    [self.boxView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if (self.model.pk_name.count) {
        __block CGFloat btnLeft = 0;
        __block CGFloat btnTop = 0;
        CGFloat margin = 15;
        CGFloat btnW = (ScreenW - 30 - 30) * 0.33333;
        CGFloat btnH = 32;
        __block CGFloat tipH = 0;
        __weak_self;
        [self.model.pk_name enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton *btn = [UIButton new];
            btn.nim_left = btnLeft;
            btn.nim_top = btnTop;
            btn.nim_width = btnW;
            btn.nim_height = btnH;
            if (btnLeft + btn.nim_width > weakself.boxView.nim_width) {
                btn.nim_left = 0;
                btn.nim_top += btnH + 5;
            }
            btn.backgroundColor = HexColor(0xF7F7FA);
            [btn setTitle:obj forState:0];
            [btn setTitleColor:Color_ThemeColor forState:0];
            btn.titleLabel.font = BoldFontOfSize(12);
            btn.layer.masksToBounds = YES;
            btn.layer.cornerRadius = 4;
            btn.tag = idx;
//            [btn addTarget:self action:@selector(tipBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [weakself.boxView addSubview:btn];
            btnLeft = btn.nim_right + margin;
            btnTop = btn.nim_top;
            tipH = btn.nim_bottom;
        }];
        self.boxView.nim_height = tipH;
        self.height = self.boxView.nim_bottom + 15;
    }else {
        self.boxView.nim_height = 0;
        self.height = self.boxTitleLab.nim_bottom + 15;
    }
}

@end
